﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Microsoft.TeamFoundation.Build.Client;
using System.Diagnostics;
using System.IO;

namespace TeamDeploy.Activities
{
    [BuildActivity(HostEnvironmentOption.Agent)]
    public sealed class XCopy: CustomActivityBase
    {
        [RequiredArgument]
        public InArgument<string> TargetMachine { get; set; }

        [RequiredArgument]
        public InArgument<string> RemoteExecutePathFilename { get; set; }

        [RequiredArgument]
        public InArgument<string> SourceSharePath { get; set; }
        
        [RequiredArgument]
        public InArgument<string> DestinationLocalPath { get; set; }

        public InArgument<string> OverwriteReadOnlyFiles { get; set; }

        public InArgument<string> ExcludeSubdirectories { get; set; }

        public InArgument<string> ExtraArgs { get; set; }


        protected override void Execute(CodeActivityContext context)
        {
            // Obtain the runtime value of the Text input argument
            string remoteExecutePathFilename = context.GetValue(this.RemoteExecutePathFilename);
            string targetMachine = context.GetValue(this.TargetMachine);
            string sourceSharePath = context.GetValue(this.SourceSharePath);
            string destinationLocalPath = context.GetValue(this.DestinationLocalPath);
            string extraArgs = context.GetValue(this.ExtraArgs);

            bool ignoreExitCode = false;
            bool.TryParse(context.GetValue(this.IgnoreExitCode), out ignoreExitCode);

            bool overwriteReadOnlyFiles = false;
            bool.TryParse(context.GetValue(this.OverwriteReadOnlyFiles), out overwriteReadOnlyFiles);

            bool excludeSubdirectories = false;
            bool.TryParse(context.GetValue(this.ExcludeSubdirectories), out excludeSubdirectories);

            string overwriteReadOnlyFilesArg = overwriteReadOnlyFiles ? " /R" : "";
            string excludeSubdirectoriesArg = excludeSubdirectories ? "" : " /S";

            if (extraArgs != null && extraArgs.Length > 0)
            {
                extraArgs = extraArgs.StartsWith(" ") ? extraArgs : " " + extraArgs;
            }
            else
            {
                extraArgs = "";
            }
            try
            {
                string processArgs = "\\\\" + targetMachine + " -accepteula CMD /C XCOPY " + sourceSharePath + " " + destinationLocalPath + " /I /Y " + overwriteReadOnlyFilesArg + excludeSubdirectoriesArg  + extraArgs;

                // Log.LogMessage("Delete Folder: " + path);
                using (System.Diagnostics.Process process = new System.Diagnostics.Process())
                {
                    process.StartInfo.FileName = remoteExecutePathFilename;
                    //process.StartInfo.WorkingDirectory = SearchPathRoot.Get(context);                    
                    process.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
                    process.StartInfo.UseShellExecute = false;
                    process.StartInfo.ErrorDialog = false;
                    process.StartInfo.CreateNoWindow = true;
                    process.StartInfo.RedirectStandardOutput = true;
                    process.StartInfo.Arguments = processArgs;

                    try
                    {
                        process.Start();
                        string output = process.StandardOutput.ReadToEnd();
                        process.WaitForExit();
                        LogMessage(context, "Delete Fold Output:" + output);
                    }
                    catch (Exception ex)
                    {
                        LogMessage(context, ex.ToString());
                        throw ex;
                    }


                    //Process.Start(remoteExecutePathFilename, processArgs);
                }
            }
            catch (Exception)
            {
                // Log.LogMessage(string.Format("Could not delete folder'{0}' on machine {1}.", path, machineName));
                throw;
            }
        }
    }
}
